﻿$(document).ready(function () {
    $("#btnSave:first").click(function (e) {
        e.preventDefault();
        var lblMessage = $("#lblMessage:first").text("");
        $(".required:visible").each(function () {
            $(this).removeClass("ui-state-error");
            if ($(this).val() == "") {
                lblMessage.text("É necessário preencher os campos obrigatórios.");
                $(this).addClass("ui-state-error");
            }
        });

        if (lblMessage.text() == "") {
            var vehicles = "";
            $("#cboVeiculosVinculados option").each(function () {
                vehicles += $(this).val() + ";";
            });
            var students = "";
            $("#cboAlunosVinculados option").each(function () {
                students += $(this).val() + ";";
            });
            var customers = "";
            $("#cboClientesVinculados option").each(function () {
                customers += $(this).val() + ";";
            });
            var documents = "";
            $("#cboFiles option").each(function () {
                documents += $.trim($(this).text()) + ";";
            });
            $.ajax({
                dataType: "json",
                type: "POST",
                url: "/Trip/Save/" + $("#hdfIdTrip").val(),
                data: {
                    code: $("#hdfCode").val(),
                    origin: $("#txtOrigin").val(),
                    destination: $("#txtDestination").val(),
                    description: $("#txtDescription").val(),
                    begin_date: $("#txtBeginDate").val(),
                    begin_time: $("#txtBeginHour").val(),
                    end_date: $("#txtEndDate").val(),
                    end_time: $("#txtEndHour").val(),
                    vehicles: vehicles,
                    students: students,
                    customers: customers,
                    documents: documents
                },
                success: function (json) {
                    document.location.href = "/Trip/Index";
                },
                error: function (json) {
                    jAlert("Erro", "Erro", function () { return false; });
                }
            });
        };
    });

    var moveRight = function (e) {
        e.preventDefault();
        var cbo = $("#cboVeiculos option:selected");
        if ($.trim(cbo.val()) != "") {
            var options = "";
            $("#cboVeiculos option:selected").each(function () {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            });
            if (options != "") {
                $("#cboVeiculosVinculados").append(options);
            }
        }
    };

    var moveLeft = function (e) {
        e.preventDefault();
        var cbo = $("#cboVeiculosVinculados option:selected");
        if ($.trim(cbo.val()) != "") {
            var options = "";
            $("#cboVeiculosVinculados option:selected").each(function () {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            });
            if (options != "") {
                $("#cboVeiculos").append(options);
            }
        }
    }

    var moveAll = function (e) {
        e.preventDefault();
        var options = "";
        $("#cboVeiculos option").each(function () {
            if ($.trim($(this).val()) != "") {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            }
        });
        if (options != "") {
            $("#cboVeiculosVinculados").append(options);
        }
    }

    var backAll = function (e) {
        e.preventDefault();
        var options = "";
        $("#cboVeiculosVinculados option").each(function () {
            if ($.trim($(this).val()) != "") {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            }
        });
        if (options != "") {
            $("#cboVeiculos").append(options);
        }
    }

    $("#lnkMoveRight").click(function (e) {
        moveRight(e);
    });

    $("#lnkMoveLeft").click(function (e) {
        moveLeft(e);
    });

    $("#lnkMoveAll").click(function (e) {
        moveAll(e);
    });

    $("#lnkBackAll").click(function (e) {
        backAll(e);
    });

    var moveRightStudent = function (e) {
        e.preventDefault();
        var cbo = $("#cboAlunos option:selected");
        if ($.trim(cbo.val()) != "") {
            var options = "";
            $("#cboAlunos option:selected").each(function () {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            });
            if (options != "") {
                $("#cboAlunosVinculados").append(options);
            }
        }
    };

    var moveLeftStudent = function (e) {
        e.preventDefault();
        var cbo = $("#cboAlunosVinculados option:selected");
        if ($.trim(cbo.val()) != "") {
            var options = "";
            $("#cboAlunosVinculados option:selected").each(function () {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            });
            if (options != "") {
                $("#cboAlunos").append(options);
            }
        }
    }

    var moveAllStudent = function (e) {
        e.preventDefault();
        var options = "";
        $("#cboAlunos option").each(function () {
            if ($.trim($(this).val()) != "") {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            }
        });
        if (options != "") {
            $("#cboAlunosVinculados").append(options);
        }
    }

    var backAllStudent = function (e) {
        e.preventDefault();
        var options = "";
        $("#cboAlunosVinculados option").each(function () {
            if ($.trim($(this).val()) != "") {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            }
        });
        if (options != "") {
            $("#cboAlunos").append(options);
        }
    }

    $("#lnkMoveRightStudent").click(function (e) {
        moveRightStudent(e);
    });

    $("#lnkMoveLeftStudent").click(function (e) {
        moveLeftStudent(e);
    });

    $("#lnkMoveAllStudent").click(function (e) {
        moveAllStudent(e);
    });

    $("#lnkBackAllStudent").click(function (e) {
        backAllStudent(e);
    });

    var moveRightCustomer = function (e) {
        e.preventDefault();
        var cbo = $("#cboClientes option:selected");
        if ($.trim(cbo.val()) != "") {
            var options = "";
            $("#cboClientes option:selected").each(function () {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            });
            if (options != "") {
                $("#cboClientesVinculados").append(options);
            }
        }
    };

    var moveLeftCustomer = function (e) {
        e.preventDefault();
        var cbo = $("#cboClientesVinculados option:selected");
        if ($.trim(cbo.val()) != "") {
            var options = "";
            $("#cboClientesVinculados option:selected").each(function () {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            });
            if (options != "") {
                $("#cboClientes").append(options);
            }
        }
    }

    var moveAllCustomer = function (e) {
        e.preventDefault();
        var options = "";
        $("#cboClientes option").each(function () {
            if ($.trim($(this).val()) != "") {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            }
        });
        if (options != "") {
            $("#cboClientesVinculados").append(options);
        }
    }

    var backAllCustomer = function (e) {
        e.preventDefault();
        var options = "";
        $("#cboClientesVinculados option").each(function () {
            if ($.trim($(this).val()) != "") {
                options += "<option value='" + $.trim($(this).val()) + "' title='" + $.trim($(this).text()) + "' >" + $.trim($(this).text()) + "</option>";
                $(this).remove();
            }
        });
        if (options != "") {
            $("#cboClientes").append(options);
        }
    }

    $("#lnkMoveRightCustomer").click(function (e) {
        moveRightCustomer(e);
    });

    $("#lnkMoveLeftCustomer").click(function (e) {
        moveLeftCustomer(e);
    });

    $("#lnkMoveAllCustomer").click(function (e) {
        moveAllCustomer(e);
    });

    $("#lnkBackAllCustomer").click(function (e) {
        backAllCustomer(e);
    });

    $("#btnAdd:first").click(function (e) {
        e.preventDefault();
        var file = $("#txtFile:first").val();
        if (file != "") {
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });

            $.ajaxFileUpload({
                url: '../../AjaxFileUpload.ashx',
                secureuri: true,
                fileElementId: 'txtFile',
                dataType: 'json',
                data: { name: 'logan', id: 'id' },
                success: function (data, status) {
                    if (typeof (data.error) != 'undefined') {
                        if (data.error != '') {
                            alert(data.error);
                        } else {
                            alert(data.msg);
                        }
                    } else {
                        if (data != null) {
                            var strFile = data.Name;
                            if (strFile != "") {
                                var options = "";
                                options += "<option value='" + $.trim(strFile) + "' title='" + $.trim(strFile) + "' >" + $.trim(strFile) + "</option>";
                                if (options != "") {
                                    $("#cboFiles:first").append(options);
                                }
                                $("#txtFile:first").val("");
                            }
                        }
                    }
                },
                error: function (data, status, e) {
                    jAlert(e, "Erro", function () { return false; });
                }
            });
        }
        return false;
    });

    $("#btnDel:first").click(function (e) {
        e.preventDefault();
        var documents = "";
        var idTrip = $("#hdfIdTrip:first").val();
        $("#cboFiles option").each(function () {
            if ($(this).attr("selected")) {
                documents += $.trim($(this).text());
                if (documents != "")
                    documents += ";";
            }
        });

        if (documents != "") {
            $.ajax({
                dataType: "json",
                type: "POST",
                url: "/Trip/DeleteFile",
                data: { idTrip: idTrip, documents: documents },
                success: function (response) {
                    if (response != null) {
                        if (response.Success) {
                            $("#cboFiles option").each(function () {
                                if ($(this).attr("selected"))
                                    $(this).remove();
                            });
                        } else {
                            jAlert("Não foi possível remover o(s) documento(s) selecionado(s).", "Alerta", function () { return false; });
                        }
                    }
                },
                error: function (response) {
                    jAlert("Erro", "Erro", function () { return false; });
                }
            });
        }
    });

    $("#txtBeginHour:first").timeEntry({ show24Hours: true });
    $("#txtEndHour:first").timeEntry({ show24Hours: true });
    $("#txtOrigin:first").focus();
});